package com.hlf.modules.h5.controller;

import com.hlf.common.annotation.SysLog;
import com.hlf.common.constants.Constants;
import com.hlf.common.utils.PropertieUtil;
import com.hlf.common.utils.ReturnResult;
import com.hlf.common.validator.Assert;
import com.hlf.modules.api.entity.CartEntity;
import com.hlf.modules.api.service.CartService;
import com.hlf.modules.h5.annotation.LoginUser;
import com.hlf.modules.merchant.order.service.OrderService;
import com.hlf.modules.wechat.model.Customer;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Created by Administrator on 2017/11/5.
 */

@Api("H5购物车接口")
@RestController
@RequestMapping("/h5/cart/")
public class H5CartController {

    @Autowired
    private CartService cartService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private PropertieUtil propertieUtil;

    /**
     * 添加购物车/修改数量
     */
    @SysLog("h5添加/修改购物车")
    @PostMapping("addCart")
    @ApiOperation(value = "h5添加购物车/修改数量", notes = "h5添加购物车/修改数量")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "goodsId", value = "商品id", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "goodsNum", value = "商品数量", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "token", value = "token", required = true)
    })
    public ReturnResult addCart(@LoginUser Customer customer, String goodsId, String goodsNum, HttpServletRequest request) {
        Assert.isBlank(goodsId, "商品id不能为空");
        Assert.isBlank(goodsNum, "商品数量不能为空");
        String tableId = String.valueOf(customer.getSpendTableId());
        cartService.createCart(goodsId, goodsNum, tableId, request);
        return ReturnResult.ok();
    }

    /**
     * 清空购物车
     */
    @SysLog("h5清空购物车")
    @GetMapping("delCart")
    @ApiOperation(value = "h5清空购物车", notes = "h5清空购物车")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "token", value = "token", required = true)
    })
    public ReturnResult cleanCart(@LoginUser Customer customer) {
        Long tableId = customer.getSpendTableId();
        cartService.deleteCartByTableId(tableId);
        return ReturnResult.ok();
    }

    /**
     * h5购物车列表
     */
    @GetMapping("cartList")
    @ApiOperation(value = "h5购物车列表", notes = "h5购物车列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "token", value = "token", required = true)
    })
    public ReturnResult updateGoodsNum(@LoginUser Customer customer) {

        Long tableId = customer.getSpendTableId();
        List<CartEntity> cartEntityList = cartService.queryCartListByTableId(tableId);
        cartEntityList = cartEntityList.stream().filter(cartEntity -> cartEntity.getGoodsTypeId() != 1).collect(Collectors.toList());
        Map<String, Object> map = new HashedMap();
        map.put(Constants.DEFAULT_RESULT_NAME, cartEntityList);
        return ReturnResult.ok(map);
    }

    /**
     * h5批量添加购物车
     */
    @SysLog("h5批量添加购物车")
    @PostMapping("batchCreate")
    @ApiOperation(value = "批量添加购物车", notes = "批量添加购物车")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "goodsInfo", value = "商品信息", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "token", value = "token", required = true)
    })
    @Transactional
    public ReturnResult batchCreateCart(@LoginUser Customer customer, @RequestParam("goodsInfo") List<String> goodsInfo, HttpServletRequest request, HttpServletResponse response) throws IOException {
        Long spendTableId = customer.getSpendTableId();
        Integer count = orderService.queryCountByTableId(spendTableId);
        if (count == 1) {
            String endUrl = propertieUtil.getPropertie("FRONT_END_URL");
            endUrl = endUrl + "/#/OrderConfirm";
            response.sendRedirect(endUrl);
            return ReturnResult.ok();
        }
        Assert.isNull(goodsInfo, "商品信息不能为空");
        String tableId = String.valueOf(spendTableId);
        cartService.batchCreateCart(goodsInfo, tableId, request);
        ReturnResult returnResult = orderService.h5CreateOrder(request, tableId, customer.getCustomerId());
        return returnResult;
    }

}
